<!--基础页面：可设置背景色；默认判断缓存里面是否有access_token-->

<script>
	import Vue from 'vue';
	import {Toast} from 'vant';
	Vue.use(Toast);
	export default {
		name: "BaseView",

		data() {
			return {
				title: "宝库系统配送端",
				defaultColor: "#3CCC77", //主题色
				colorBtn:'linear-gradient(to right,#48E267,#3CCC77)',//vant按钮颜色
				backcolor: "#F5F5F5", //背景色
				imageUrl: "https://erpbaoku.oss-cn-chengdu.aliyuncs.com//",
				qrCode: "https://fcxcx.kmmyxb.net.cn/mht/", //名片二维码
				emptyImg: require("@/assets/images/empty.png"),
				pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
				// before_backcolor:'',//进入此页面之前的背景色
			}
		},
		methods: {
			/** method post 或者get
			 *url 接口地址
			 * params 参数
			 * comfunc 访问成功执行的方法
			 * loading_tip 加载中的提示文本
			 */
			request(method, url, params, comfunc, loading_tip = "") {
				// Toast.clear()
				if (loading_tip) {
					// thia.$router.replace()
					this.$toast.loading({
						message: loading_tip,
						forbidClick: true,
						duration: 0,
					});
				}

				var access_token = localStorage.getItem("access_token")
				var that = this;
				this.$http({
					url: url,
					method: method,
					data: params,
					params: method == "GET" || method == "get" ? params : "",
					headers: {
						'Authorization': access_token
					} //设置header信息
				}).then((res) => {
					this.$toast.clear();
					if (res.headers.authorization) {
						localStorage.setItem('access_token', res.headers.authorization);
						// that.request(method, url, params, comfunc, loading_tip,false );
						// return;
					}
					var data = res.data;

					if (data.code == 401) {
						// https://fcxcx.kmmyxb.net.cn/mht/#/personal
						// localStorage.setItem('redirect_uri', window.location.href);
						// "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1ad4d1d3f36426ed&redirect_uri="+window.location.href+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
						// that.$toast(data.msg);
						that.$router.push("pwdlogin");
					} else if (data.code == 2001 || data.code == 2002) { //预支付编号有误，返回首页
						that.$toast(data.msg);
						that.$router.push("/");
					} else if (data.code != 0) {
						that.$toast(data.msg);
						comfunc(data.code);
					} else { //成功
						if (loading_tip) {
							// Toast.clear()
						}
						comfunc(data.result);
					}


					// console.log('that.shop_info',this.shop_info);
				}).catch(function(error) {
					// this.$toast.clear()
					console.log(error);
					// console.log(error.response.data.errors);
					if (!error.response) {
						return;
					}
					if (error.response.status == 401) {
						// var code = that.getUrlParam("code")
						// if (code != null) {
						// 	that.login(code);
						// 	return;
						// };
						// that.$toast("登陆失效");
						// let redirect_uri = encodeURIComponent(window.location.href)
						// window.location.href =
						// 	"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1ad4d1d3f36426ed&redirect_uri=" + redirect_uri +
						// 	"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"

						// that.$router.push("pwdlogin");
					} else {
						if (error.response.data && error.response.data.errors) {
							that.$toast(url + "：" + JSON.stringify(error.response.data.errors));
						} else {
							that.$toast(url + "：" + error);
						}

					}
					// console.log(error.response.status);
					// console.log(error);
				});
			},
			// 获取url上的参数
			getUrlParam(name) {
				let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
				let r = window.location.search.substr(1).match(reg); //匹配目标参数
				if (r != null) return decodeURIComponent(r[2]);
				return null; //返回参数值　
			},
			escape2Html(str) {
				var arrEntities = {
					'lt': '<',
					'gt': '>',
					'nbsp': ' ',
					'amp': '&',
					'quot': '"'
				};
				return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) {
					return arrEntities[t];
				});
			},
			//时间
			/**
			 * @param intervalDays ：间隔天数
			 */
			getDateRange(intervalDays) {
				let oneDayTime = 24 * 60 * 60 * 1000;
				var dateNow = new Date();
				let list = [];
				let lastDay;
				lastDay = new Date(dateNow.getTime() - intervalDays * oneDayTime);
				list.push(this.formateDate(lastDay));
				list.push(this.formateDate(dateNow));

				return list;
			},
			//时间格式化
			formateDate(time) {
				let year = time.getFullYear()
				let month = time.getMonth() + 1
				let day = time.getDate()
				return year + '-' + month 
			}
		},
		beforeDestroy: function() {
			// document.body.style.backgroundColor = "#FFFFFF";
			// document.body.style.backgroundColor = this.before_backcolor;
			// console.log("恢复背景色");
		},
		mounted: function() {
			document.title = this.title; //设置页面title
			// // if(!this.$route.params.no_token){//是否检测token,没有的话，跳转登陆页面
			// //    var access_token = localStorage.getItem("access_token");
			// //    if (!access_token) {
			// //        console.log("未登陆，跳转登陆页面");
			// //        this.$router.push("pwdlogin");
			// //    } 
			// // }


			document.body.style.backgroundColor = this.backcolor;
			// console.log("设置背景色", this.backcolor);
			// check_code();
		},
		// check_code(){
		// 	this.$route.query.code
		// }

	}
</script>
